<template>
  <div>
    <div class="clearflex">
      <el-button @click="relation" class="fl-right" size="small" type="primary">确 定</el-button>
    </div>
    <el-tree
      :data="menuTreeData"
      :default-checked-keys="menuTreeIds"
      :props="menuDefaultProps"
      @check="nodeChange"
      default-expand-all
      highlight-current
      node-key="menuId"
      ref="menuTree"
      show-checkbox
    ></el-tree>
  </div>
</template>
<script>
import { getBaseMenuTree,addMenuAuthority,getMenuAuthority} from '@/api/menu'

export default {
  name: 'Menus',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      menuTreeData: [],
      menuTreeIds: [],
      needConfirm:false,
      menuDefaultProps: {
        children: 'childList',
        label: function(data){
          return data.title
        }
      }
    }
  },
  methods: {
    nodeChange(){
      this.needConfirm = true
    },
    // 暴露给外层使用的切换拦截统一方法
    enterAndNext(){
      this.relation()
    },
    // 关联树 确认方法
    async relation() {
      const checkArr = this.$refs.menuTree.getCheckedNodes(false)
      var idsStr = checkArr.map(function(obj,index){
        return obj.menuId;
      }).join(",").split(',');

      const res = await addMenuAuthority({
        menuId: idsStr,
        roleId: this.row.roleId
      })
      if (res.statusCode == 0) {
        this.$message({
          type: 'success',
          message: '菜单设置成功!'
        })
      }
    }
  },
  async created() {
    // 获取所有菜单树
    const res = await getBaseMenuTree()
    this.menuTreeData = res.data
    const res1 = await getMenuAuthority(this.row.roleId)
    this.menuTreeIds = res1.data;

  }
}
</script>
<style lang="scss">
</style>